package zhongbo.java.air.monitor.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import zhongbo.java.air.common.exception.MyException;
import zhongbo.java.air.common.vo.PageVo;
import zhongbo.java.air.monitor.mapper.FlightActivityDailyMapper;
import zhongbo.java.air.monitor.model.AirAlertLog;
import zhongbo.java.air.monitor.model.FlightActivityDaily;
import zhongbo.java.air.monitor.service.IFlightActivityDailyService;

import java.util.List;

/**
 * 说明：
 *
 * @author 钟波dellNotebook
 * @since 2025年04月08日 星期二 04:07:41
 */
@Service
@Slf4j
public class FlightActivityDailyServiceImpl implements IFlightActivityDailyService {

    @Autowired
    private FlightActivityDailyMapper flightActivityDailyMapper;

    @Override
    public List<FlightActivityDaily> selectDailyStats(String date) {
        return flightActivityDailyMapper.selectDailyStats(date);
    }

    @Override
    public List<FlightActivityDaily> getDailyStats(String date) {
        if (StrUtil.isBlank(date)) {
            throw new RuntimeException("data参数必须填写!示例: 2025-04-08");
        }
        QueryWrapper<FlightActivityDaily> queryWrapper= new QueryWrapper<>();
        queryWrapper.eq("date",date);
        return flightActivityDailyMapper.selectList(queryWrapper);
    }

    @Override
    public FlightActivityDaily addFlightActivityDaily(FlightActivityDaily flightActivityDaily) throws MyException {
        int ok = flightActivityDailyMapper.insert(flightActivityDaily);
        if (ok <= 0) {
            throw new MyException("添加失败!");
        }
        QueryWrapper<FlightActivityDaily> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", flightActivityDaily.getId());
        return flightActivityDailyMapper.selectOne(queryWrapper);
    }

    @Override
    public FlightActivityDaily updateFlightActivityDaily(FlightActivityDaily flightActivityDaily) throws MyException {
        int ok = flightActivityDailyMapper.updateById(flightActivityDaily);
        if (ok <= 0) {
            throw new MyException("更新失败!");
        }
        return flightActivityDailyMapper.selectById(flightActivityDaily.getId());
    }

    @Override
    public void deleteFlightActivityDaily(int id) throws MyException {
        int ok = flightActivityDailyMapper.deleteById(id);
        if (ok <= 0) {
            throw new MyException("删除失败!");
        }

    }

    @Override
    public PageVo<FlightActivityDaily> page(Integer pageNumber, Integer pageSize, boolean order, String searchField, String keyword, String sortField) {
        QueryWrapper<FlightActivityDaily> queryWrapper = new QueryWrapper<>();
        if (StrUtil.isNotBlank(keyword) && StrUtil.isNotBlank(searchField)) {
            queryWrapper.like(searchField, keyword);
        }
        if (StrUtil.isNotBlank(sortField)) {
            if (order) {
                queryWrapper.orderByAsc(sortField);
            } else {
                queryWrapper.orderByDesc(sortField);
            }
        }

        IPage<FlightActivityDaily> pageParam = new Page<>(pageNumber, pageSize);
        IPage<FlightActivityDaily> resultPage = flightActivityDailyMapper.selectPage(pageParam, queryWrapper);
        return new PageVo<>(resultPage);
    }

    @Override
    public FlightActivityDaily selectFlightActivityDaily(String Id) throws MyException {
       FlightActivityDaily flightActivityDaily= flightActivityDailyMapper.selectById(Id);
        if(flightActivityDaily==null){
            throw new MyException("未找到飞机记录!");
        }
        return flightActivityDaily;
    }
    @Override
    public PageVo<FlightActivityDaily> pageByIcaoCode(Integer pageNumber, Integer pageSize, String icaoCode, String startTime, String endTime, String oderByField, boolean orderByAsc, String keyword, String searchField) throws MyException {
        QueryWrapper<FlightActivityDaily> queryWrapper = new QueryWrapper<>();
        if (StrUtil.isNotBlank(icaoCode)) {
            queryWrapper.eq("icao_code", icaoCode);
        }
        if (StrUtil.isNotBlank(keyword) && StrUtil.isNotBlank(searchField)) {
            String[]fieldsArray = searchField.split(",");
            for (String field : fieldsArray) {
                queryWrapper.or().like(StrUtil.toUnderlineCase(field), keyword);
            }
        }
        if (StrUtil.isNotBlank(startTime) && StrUtil.isNotBlank(endTime)) {
            queryWrapper.between("date", startTime, endTime);
        }
        if(StrUtil.isNotBlank(oderByField)){
            if (orderByAsc) {
                queryWrapper.orderByAsc(oderByField);
            } else {
                queryWrapper.orderByDesc(oderByField);
            }
        }
        IPage<FlightActivityDaily> pageParam = new Page<>(pageNumber, pageSize);
        IPage<FlightActivityDaily> resultPage = flightActivityDailyMapper.selectPage(pageParam, queryWrapper);
        return new PageVo<>(resultPage);
    }


}
